﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;

namespace CheckFreeWayClient
{
    public partial class Principal : Form
    {
        string pathLog = @".\";

        public Principal()
        {
            InitializeComponent();
        }

        private void btnChecarAgora_Click(object sender, EventArgs e)
        {
            TestaConexoes tc = new TestaConexoes(txtHostName.Text, Convert.ToInt32(txtPortaInicial.Text), Convert.ToInt32(txtPortaFinal.Text));
            
            tc.OnSendMessage += new TestaConexoes.OnSendMessageDelegate(tc_OnSendMessage);
            tc.OnReceiveMessage += new TestaConexoes.OnReceiveMessageDelegate(tc_OnReceiveMessage);
            tc.OnErrorConnecting += new TestaConexoes.OnErrorConnectingDelegate(tc_OnErrorConnecting);
            tc.OnErrorReceiving += new TestaConexoes.OnErrorReceivingErrorDelegate(tc_OnErrorReceiving);
            tc.OnErrorSending += new TestaConexoes.OnErrorSendingErrorDelegate(tc_OnErrorSending);
            tc.OnPortChecked += new TestaConexoes.OnPortCheckedDelegate(tc_OnPortChecked);
            tc.OnEndPortCheck += new TestaConexoes.OnEndPortCheckDelegate(tc_OnEndPortCheck);

            tcTesta d = new tcTesta(tc.Testa);
            this.BeginInvoke(d, new object[] { txtMensagem.Text });

            //tc.Testa(txtMensagem.Text);
        }

        void tc_OnEndPortCheck()
        {
            txtChecking.Text = "";
        }

        private delegate void tcTesta(string str);

        void tc_OnPortChecked(int port)
        {
            txtChecking.Text = "Checking Port " + port.ToString() + " ...";
        }

        void tc_OnErrorSending(Exception ex, int port, string hostname)
        {
            File.AppendAllText(pathLog + "Error Sending" + port.ToString() + ".txt", "PORTA - HOSTNAME: " + port.ToString() + "  -  " + hostname + "\r\n");
            File.AppendAllText(pathLog + "Error Sending" + port.ToString() + ".txt", ex.Message + "\r\n");
            File.AppendAllText(pathLog + "Error Sending" + port.ToString() + ".txt", ex.StackTrace + "\r\n");
            File.AppendAllText(pathLog + "Error Sending" + port.ToString() + ".txt", "-------------------------------------------------------------------------------------------------\r\n");

            File.AppendAllText(pathLog + "General Error.txt", "SENDING porta-hostname: " + port.ToString() + "  -  " + hostname + "\r\n");
            File.AppendAllText(pathLog + "General Error.txt", ex.Message + "\r\n");
            File.AppendAllText(pathLog + "General Error.txt", ex.StackTrace + "\r\n");
            File.AppendAllText(pathLog + "General Error.txt", "-------------------------------------------------------------------------------------------------\r\n");

            File.AppendAllText(pathLog + "General Report.txt", "OnErrorSending ; " + port.ToString() + " ; " + hostname + "\r\n");
            File.AppendAllText(pathLog + "General Report.txt", ex.StackTrace +"\r\n");

        }

        void tc_OnErrorReceiving(Exception ex, int port, string hostname)
        {
            File.AppendAllText(pathLog + "Error Receiving" + port.ToString() + ".txt", "PORTA - HOSTNAME: " + port.ToString() + "  -  " + hostname + "\r\n");
            File.AppendAllText(pathLog + "Error Receiving" + port.ToString() + ".txt", ex.Message + "\r\n");
            File.AppendAllText(pathLog + "Error Receiving" + port.ToString() + ".txt", ex.StackTrace + "\r\n");
            File.AppendAllText(pathLog + "Error Receiving" + port.ToString() + ".txt", "-------------------------------------------------------------------------------------------------\r\n");

            File.AppendAllText(pathLog + "General Error.txt", "RECEIVING porta-hostname: " + port.ToString() + "  -  " + hostname + "\r\n");
            File.AppendAllText(pathLog + "General Error.txt", ex.Message + "\r\n");
            File.AppendAllText(pathLog + "General Error.txt", ex.StackTrace + "\r\n");
            File.AppendAllText(pathLog + "General Error.txt", "-------------------------------------------------------------------------------------------------\r\n");

            File.AppendAllText(pathLog + "General Report.txt", "OnErrorReceiving ; " + port.ToString() + " ; " + hostname + "\r\n");
            File.AppendAllText(pathLog + "General Report.txt", ex.StackTrace +"\r\n");

        }

        void tc_OnErrorConnecting(Exception ex, int port, string hostname)
        {
            File.AppendAllText(pathLog + "Error Connecting" + port.ToString() + ".txt", "PORTA - HOSTNAME: " + port.ToString() + "  -  " + hostname + "\r\n");
            File.AppendAllText(pathLog + "Error Connecting" + port.ToString() + ".txt", ex.Message + "\r\n");
            File.AppendAllText(pathLog + "Error Connecting" + port.ToString() + ".txt", ex.StackTrace + "\r\n");
            File.AppendAllText(pathLog + "Error Connecting" + port.ToString() + ".txt", "-------------------------------------------------------------------------------------------------\r\n");

            File.AppendAllText(pathLog + "General Error.txt", "CONNECTING porta-hostname: " + port.ToString() + "  -  " + hostname + "\r\n");
            File.AppendAllText(pathLog + "General Error.txt", ex.Message + "\r\n");
            File.AppendAllText(pathLog + "General Error.txt", ex.StackTrace + "\r\n");
            File.AppendAllText(pathLog + "General Error.txt", "-------------------------------------------------------------------------------------------------\r\n");

            File.AppendAllText(pathLog + "General Report.txt", "OnErrorConnecting ; " + port.ToString() + " ; " + hostname + "\r\n");
            File.AppendAllText(pathLog + "General Report.txt", ex.StackTrace +"\r\n");

        }

        void tc_OnReceiveMessage(string strMessage, int port, string hostname)
        {
            File.AppendAllText(pathLog + "Received Message.txt", "porta-hostname: " + port.ToString() + "  -  " + hostname + strMessage +"\r\n");

            File.AppendAllText(pathLog + "General Report.txt", "OnReceiveMessage ; " + port.ToString() + " ; " + hostname + "\r\n");
            File.AppendAllText(pathLog + "General Report.txt", strMessage +"\r\n");
        }

        void tc_OnSendMessage(string strMessage, int port, string hostname)
        {
            File.AppendAllText(pathLog + "Sended Message.txt", "porta-hostname: " + port.ToString() + "  -  " + hostname + strMessage +"\r\n");

            File.AppendAllText(pathLog + "General Report.txt", "OnSendMessage ; " + port.ToString() + " ; " + hostname + "\r\n");
            File.AppendAllText(pathLog + "General Report.txt", strMessage +"\r\n");
        }

        private void Principal_Load(object sender, EventArgs e)
        {

        }

        private void btnGerarBuffer_Click(object sender, EventArgs e)
        {
            String strBuffer = String.Empty;
            StringBuilder sb = new StringBuilder();
            int tamanho = Convert.ToInt32(txtTamanhoBuffer.Text);

            sb.Append(">");
            for (int idx = 1; idx < tamanho - 1; idx++)
                sb.Append("*");
            sb.Append("<");

            strBuffer = sb.ToString();

            txtMensagem.Text = strBuffer;
        }

    }
}
